#!/bin/bash
set -e

TOP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )"
SCRIPTS_DIR="${TOP_DIR}/scripts"
PACKAGE_DIR="${TOP_DIR}/package"

source $SCRIPTS_DIR/version

cd $TOP_DIR

mkdir -p dist/artifacts
cp bin/harvester dist/artifacts/harvester${SUFFIX}

cd $PACKAGE_DIR

IMAGE=${REPO}/harvester:${TAG}
DOCKERFILE=Dockerfile
if [ -e ${DOCKERFILE}.${ARCH} ]; then
    DOCKERFILE=${DOCKERFILE}.${ARCH}
fi

rm -rf ./harvester
cp ../bin/harvester .

docker build --build-arg VERSION=${VERSION} --build-arg ARCH=${ARCH} -f ${DOCKERFILE} -t ${IMAGE} .
echo Built ${IMAGE}

IMAGE_PUSH=${REPO}/harvester:${IMAGE_PUSH_TAG}
docker tag "${IMAGE}" "${IMAGE_PUSH}"
echo Tagged "${IMAGE_PUSH}"

DOCKER_CONFIG="../.docker"
if [[ -n ${PUSH} ]];then
  docker --config=${DOCKER_CONFIG} push "${IMAGE_PUSH}"
  echo Pushed "${IMAGE_PUSH}"
fi
